//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 21
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;

namespace PrintSuggestionsInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Need to retrieve a valid SuggestionDayResult via GetUserAvailability before calling
            // PrintSuggestionsInformation
        }

        /// <summary>
        /// Displays to the console each of the suggestions that are included in the 
        /// provided SuggestionDayResult array. (Listing 21-55)
        /// </summary>
        /// <param name="suggestionDays">Array of SuggestedDayResults</param>
        /// <param name="participants">MailboxData array that was used in the 
        /// GetUserAvailabilityRequest instance.</param>
        public static void PrintSuggestionsInformation(
            SuggestionDayResult[] suggestionDays,
            MailboxData[] attendees)
        {
            foreach (SuggestionDayResult suggestionDay in suggestionDays)
            {
                Console.WriteLine("Suggestion Day ({0}): {1}",
                    suggestionDay.DayQuality,
                    suggestionDay.Date);

                foreach (Suggestion suggestion in suggestionDay.SuggestionArray)
                {
                    Console.WriteLine("Time ({0}): {1}",
                        suggestion.SuggestionQuality,
                        suggestion.MeetingTime);

                    // Display conflict information, there should be an entry 
                    // in the AttendeeConflictDataArray for each attendee.
                    int numberOfUnknownOrTooBigParticipants = 0;
                    for (int x = 0;
                        x < suggestion.AttendeeConflictDataArray.Length;
                        x++)
                    {
                        string participantName = attendees[x].Email.Name;
                        if (String.IsNullOrEmpty(participantName))
                            participantName = attendees[x].Email.Address;

                        AttendeeConflictData attendeeConflictData =
                            suggestion.AttendeeConflictDataArray[x];
                        if (attendeeConflictData is IndividualAttendeeConflictData)
                        {
                            IndividualAttendeeConflictData individualConflictData =
                                (IndividualAttendeeConflictData)attendeeConflictData;

                            Console.WriteLine("{0} is {1}",
                                participantName,
                                individualConflictData.BusyType);
                        }
                        if (attendeeConflictData is GroupAttendeeConflictData)
                        {
                            GroupAttendeeConflictData groupConflictData =
                                (GroupAttendeeConflictData)attendeeConflictData;

                            Console.WriteLine("The \"{0}\" group has:" +
                                    "\r\n\t{1} Available" +
                                    "\r\n\t{2} Conflicting" +
                                    "\r\n\t{3} With No Data Available",
                                participantName,
                                groupConflictData.NumberOfMembersAvailable,
                                groupConflictData.NumberOfMembersWithConflict,
                                groupConflictData.NumberOfMembersWithNoData);
                        }
                        if (attendeeConflictData is UnknownAttendeeConflictData ||
                            attendeeConflictData is TooBigGroupAttendeeConflictData)
                        {
                            numberOfUnknownOrTooBigParticipants++;
                        }
                    }

                    if (numberOfUnknownOrTooBigParticipants > 0)
                    {
                        Console.WriteLine("You have {0} participant(s) that are " +
                            "invalid.",
                            numberOfUnknownOrTooBigParticipants);
                    }
                }
                Console.WriteLine();
            }
        }

    }
}
